package com.android.lyricshow.ui;

import com.android.lyricshow.BuildConfig;
import com.android.lyricshow.until.AppUntil;
import com.android.lyricshow.until.LogPrint;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.support.v4.util.LruCache;
import android.util.Log;

public class ImageCache {
	
	private static final String TAG = "ImageCache";
	
	// Default memory cache size
    private static final int DEFAULT_MEM_CACHE_SIZE = 1024 * 1024; // 5MB

    // Default disk cache size
    private static final int DEFAULT_DISK_CACHE_SIZE = 1024 * 1024 * 10; // 10MB

    // Compression settings when writing images to disk cache
    private static final CompressFormat DEFAULT_COMPRESS_FORMAT = CompressFormat.JPEG;
    private static final int DEFAULT_COMPRESS_QUALITY = 70;
    private static final int DISK_CACHE_INDEX = 0;

    // Constants to easily toggle various caches
    private static final boolean DEFAULT_MEM_CACHE_ENABLED = true;
    private static final boolean DEFAULT_DISK_CACHE_ENABLED = true;
    private static final boolean DEFAULT_CLEAR_DISK_CACHE_ON_START = false;
    private static final boolean DEFAULT_INIT_DISK_CACHE_ON_CREATE = false;
    
    private LruCache<Long, Bitmap> mMemoryCache;
    
    private ImageCacheParams mCacheParams;
    
	public ImageCache(ImageCacheParams cacheParams){
		InitCache(cacheParams);
	}
	
	private void InitCache(ImageCacheParams cacheParams){
		mCacheParams = cacheParams;
		
		    // Set up memory cache
		if (mCacheParams.memoryCacheEnabled) {
		    
			if (BuildConfig.DEBUG) {
		        Log.e(TAG, "Memory cache created (size = " + mCacheParams.memCacheSize + ")");
		    }
		    
		    mMemoryCache = new LruCache<Long, Bitmap>(mCacheParams.memCacheSize) {
		        /**
		         * Measure item size in bytes rather than units which is more practical
		         * for a bitmap cache
		         */
		        @Override
		        protected int sizeOf(Long key, Bitmap bitmap) {
		            return getBitmapSize(bitmap);
		        }
		    };
		}
	}
	
	public Bitmap findBitmapById(long key){
		if(mMemoryCache != null){
			return mMemoryCache.get(key);
		}
		
		return null;
	}
	
	public void addBitmapToCache(long data, Bitmap bitmap){
		if (bitmap == null) {
            return;
        }

        // Add to memory cache
        if (mMemoryCache != null && mMemoryCache.get(data) == null) {
            mMemoryCache.put(data, bitmap);
        }
	}
	
    public static int getBitmapSize(Bitmap bitmap) {
        if (AppUntil.hasHoneycombMR1()) {
            return bitmap.getByteCount();
        }
        
        return bitmap.getRowBytes() * bitmap.getHeight();
    }
    
    public static class ImageCacheParams {
    	
        public int memCacheSize = DEFAULT_MEM_CACHE_SIZE;
        
        public CompressFormat compressFormat = DEFAULT_COMPRESS_FORMAT;
        public int compressQuality = DEFAULT_COMPRESS_QUALITY;
        public boolean memoryCacheEnabled = DEFAULT_MEM_CACHE_ENABLED;

        public void setMemCacheSizePercent(Context context, float percent) {
            if (percent < 0.05f || percent > 0.8f) {
                throw new IllegalArgumentException("setMemCacheSizePercent - percent must be "
                        + "between 0.05 and 0.8 (inclusive)");
            }
            memCacheSize = Math.round(percent * getMemoryClass(context) * 1024 * 1024);
        }

        private static int getMemoryClass(Context context) {
            return ((ActivityManager) context.getSystemService(
                    Context.ACTIVITY_SERVICE)).getMemoryClass();
        }
    }
}
